package com.aaa.three.service.impl;

import com.aaa.three.entity.*;
import com.aaa.three.mapper.PatientRegistrationMapper;
import com.aaa.three.pay.AliPayBean;
import com.aaa.three.pay.Alipay;
import com.aaa.three.pojo.domain.AdminInfo;
import com.aaa.three.pojo.domain.UserEmp;
import com.aaa.three.pojo.query.DoctorQuery;
import com.aaa.three.result.AjaxResult;

import com.aaa.three.service.PatientRegistrationService;
import com.aaa.three.sysconst.UserConst;
import com.aaa.three.util.DateUtil;
import com.aaa.three.util.QueryDate;
import com.alipay.api.AlipayApiException;

import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @PACKAGE_NAME: com.aaa.three.service.impl
 * @NAME: PatientRegistrationServiceImpl
 * @USER: Tprairie
 * @DATE: 2022/2/18
 * @PROJECT_NAME: three
 */
@Service
public class PatientRegistrationServiceImpl implements PatientRegistrationService {
    @Autowired
    private PatientRegistrationMapper patientRegistrationMapper;
    @Autowired
    private HttpSession session;
    @Override
    public AjaxResult listAll(DoctorQuery doctorQuery) {
        TableDate tableDate = DateUtil.listTitle();
        String query = QueryDate.query(0);
        doctorQuery.setNewDate(query);
        List<DoctorSchedule> doctorList =patientRegistrationMapper.listAll(doctorQuery);
        Integer sum = patientRegistrationMapper.listSum(doctorQuery);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("titleData",tableDate);
        map.put("listSum",sum);
        map.put("listDoctorAll",doctorList);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getSchedule(AddReg addReg) {
        ScheduleVo scheduleVo=patientRegistrationMapper.getSchedule(addReg);
        if(scheduleVo==null){
            return AjaxResult.fail("时段没有坐诊");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getinformation() {
        UserEmp attribute = (UserEmp) session.getAttribute(UserConst.USER_IN_SESSION);
        Integer id = attribute.getId();
        UserEmp userEmp=patientRegistrationMapper.getinformation(id);
        String idcard = userEmp.getIdcard();
        if(idcard==null){
            return AjaxResult.fail("身份信息未添加");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult verifyQuery(AddReg addReg) {
        UserEmp attribute = (UserEmp) session.getAttribute(UserConst.USER_IN_SESSION);
        Integer id = attribute.getId();
        addReg.setId(id);
        AddReg addReg1=patientRegistrationMapper.getRegistrationDoctor(addReg);
        Integer id1 = addReg1.getId();
        addReg.setSid(id1);
        AddReg addReg2=patientRegistrationMapper.getRegistration(addReg);
        if(addReg2==null){
            return AjaxResult.success();
        }
        return AjaxResult.fail("当前时间已预约");
    }

    @Override
    public AjaxResult addRegistration(AddReg addReg){
        UserEmp attribute = (UserEmp) session.getAttribute(UserConst.USER_IN_SESSION);
        Integer id = attribute.getId();
        addReg.setId(id);
        AddReg addReg1=patientRegistrationMapper.getRegistrationDoctor(addReg);
        Integer id1 = addReg1.getId();
        addReg.setSid(id1);
        Integer i=patientRegistrationMapper.addRegistration(addReg);
            if(i>0){
               if(addReg.getDay()==0){
                   Integer upname = addReg1.getUpname();
                   Integer totalnumber = addReg1.getTotalnumber();
                   Integer lastnumber = addReg1.getLastnumber();
                   Integer t=totalnumber-1;
                   Integer l=lastnumber+1;
                   Integer u=upname-1;
                   addReg.setUpname(u);
                   addReg.setTotalnumber(t);
                   addReg.setLastnumber(l);
                   Integer a= patientRegistrationMapper.updateUpSchedule(addReg);
                   return AjaxResult.success();
               }else {
                   Integer downname = addReg1.getDownname();
                   Integer totalnumber = addReg1.getTotalnumber();
                   Integer lastnumber = addReg1.getLastnumber();
                   Integer t=totalnumber-1;
                   Integer l=lastnumber+1;
                   Integer d=downname-1;
                   addReg.setDownname(d);
                   addReg.setTotalnumber(t);
                   addReg.setLastnumber(l);
                   Integer a= patientRegistrationMapper.updateDownSchedule(addReg);
                   return AjaxResult.success();
               }

            }
           return AjaxResult.fail("挂号失败");
    }

    @Override
    public AjaxResult getAdminInfo(Integer id) {
        DoctorInfo adminInfo=patientRegistrationMapper.getAdminInfo(id);
        return AjaxResult.success(adminInfo);
    }
}
